<template>
  <div class="tool-box">
    <div class="tool-box-title">
      <span>{{ title }}</span>
      <van-icon name="arrow" size="14" />
    </div>
    <div class="tool-grid">
      <div
        v-for="(tool, index) in tools"
        :key="index"
        class="tool-item"
        @click="onToolClick(tool)"
      >
        <div class="tool-icon">
          <van-icon :name="tool.icon" size="24" />
        </div>
        <div class="tool-name">{{ tool.name }}</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { defineProps, defineEmits } from 'vue'
import { useRouter } from 'vue-router'

const props = defineProps({
  title: {
    type: String,
    default: '实用工具'
  },
  tools: {
    type: Array,
    default: () => [
      { id: 'ai', name: 'AI模型', icon: 'cluster-o', route: '/ai-chat' },
      { id: 'compiler', name: '在线编译', icon: 'terminal-o', route: '/compiler' },  // 修改为 terminal-o 图标
      { id: 'exercise', name: '练习题库', icon: 'notes-o', route: '/exercise' },
      { id: 'weather', name: '天气预报', icon: 'sunny-o', route: '/weather' },
      { id: 'article', name: '创作文章', icon: 'edit', route: '/article/create' },
      { id: 'settings', name: '设置', icon: 'setting-o', route: '/settings' },
      { id: 'bookmark', name: '收藏夹', icon: 'star-o', route: '/bookmark' },
      { id: 'history', name: '浏览历史', icon: 'clock-o', route: '/history' }
    ]
  },
  columns: {
    type: Number,
    default: 4
  }
})

const emit = defineEmits(['tool-click'])
const router = useRouter()

const onToolClick = (tool) => {
  emit('tool-click', tool)
  
  if (tool.route) {
    router.push(tool.route)
  }
}
</script>

<style lang="scss" scoped>
.tool-box {
  background-color: var(--background-color-light);
  border-radius: 8px;
  padding: 12px;
  margin: 12px 0;
  box-shadow: var(--box-shadow);
  
  .tool-box-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
    font-size: 16px;
    font-weight: 500;
    color: var(--text-color);
  }
  
  .tool-grid {
    display: grid;
    grid-template-columns: repeat(v-bind(columns), 1fr);
    gap: 12px;
    
    .tool-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      padding: 12px 8px;
      border-radius: 8px;
      background-color: var(--background-color);
      cursor: pointer;
      transition: transform 0.2s, box-shadow 0.2s;
      
      &:active {
        transform: scale(0.95);
      }
      
      .tool-icon {
        margin-bottom: 8px;
        color: var(--primary-color);
      }
      
      .tool-name {
        font-size: 12px;
        color: var(--text-color);
        text-align: center;
      }
    }
  }
}
</style>